package onOroff

import (
	"sanguo/cluster"
	"sanguo/node/node_team"
	"sanguo/node/node_team/team"
	//ss_rpc "sanguo/protocol/ss/rpc"
	//cs_msg "sanguo/protocol/cs/message"
	ss_msg "sanguo/protocol/ss/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

const (
	off_line = 1
	on_line  = 2
)

func onOnOrOffLine(session kendynet.StreamSession, msg proto.Message) {
	arg := msg.(*ss_msg.OnOroffLine)
	node_team.Debugln("OnCall OnOrOffLine", arg)

	//队伍
	_team := team.TeamMgr.RoleToTeam[arg.GetRoleID()]
	if _team != nil {
		r := _team.GetRole(arg.GetRoleID())
		if r != nil {
			if arg.GetMsgType() == off_line {
				r.SetOffLineTime()
				team.UpdateTeamToClients(_team)

			} else if arg.GetMsgType() == on_line {

				r.ClearOffLineTime()
				team.UpdateTeamToClients(_team)

			}
		}
	}

	/*
		//匹配
		_matchObj := match.MatchMgr.RoleToMatch[arg.GetRoleID()]
		if _matchObj != nil {
			if arg.GetMsgType() == off_line {
				resp := &cs_msg.MatchToC{
					MsgType: cs_msg.MatchType(cs_msg.MatchType_Off_Line).Enum(),
				}

				//todo
				queue := match.MatchMgr.GetMatchQueue(1, 10)
				if queue != nil {
					obj := queue.GetQueueOjb(_matchObj.MatchID)
					if obj != nil {
						if obj.TeamID != 0 {
							_team := team.TeamMgr.GetTeam(obj.TeamID)
							_team.Status = com.Status_Safe
							for _, r := range _team.Roles {
								delete(match.MatchMgr.RoleToMatch, r.RoleID)
								r.SendToClient(resp)
							}
						} else {
							for _, r := range obj.Roles {
								delete(match.MatchMgr.RoleToMatch, r.RoleID)
								com.SendToClient(r.Game, r.UserID, resp)
							}
						}
						queue.DelQueue(_matchObj.MatchID)
					}
				}

			}
		}

	*/

	//房间

}

func init() {
	cluster.Register(&ss_msg.OnOroffLine{}, func(session kendynet.StreamSession, msg proto.Message) {
		onOnOrOffLine(session, msg)
	})
}
